package com.protect.action.key;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.net.URL;

import javax.swing.JTextField;

import org.jdesktop.jdic.browser.WebBrowser;

public class Key implements KeyListener {
	
	JTextField textfield_urladdress ;
	private WebBrowser browser;
	
	public Key(JTextField textfield_urladdress,WebBrowser browser ){
		this.textfield_urladdress = textfield_urladdress;
		this.browser = browser;
	}

	@Override
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub
		
		
	}

	@Override
	public void keyPressed(KeyEvent e) {
		// TODO Auto-generated method stub
		if(e.getKeyCode()==KeyEvent.VK_ENTER){
			String text = textfield_urladdress.getText();
			if (text == null || text.equals("")) {
			return;
			}
			if (!text.toLowerCase().startsWith("http://")) {
				text = "http://" + text;
				}
			try {
				browser.setURL(new URL(text));
				BufferedWriter bufr = 
						new BufferedWriter(new FileWriter("res/url.txt"));
				bufr.write(text);
				bufr.close();
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
				}
		}
	}

	@Override
	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}

}
